#include "ChessBoardController.h"

ChessBoardController::ChessBoardController(QWidget *parent) {
    chessBoardView  = new ChessBoardView(parent);
    chessBoard = new ChessBoard();

    connect(chessBoard, SIGNAL(repaint()), this, SLOT(refresh()));

    connect(chessBoardView, SIGNAL(mouseMoved(int,int)), this, SLOT(mouseMoved(int,int)));
}

void ChessBoardController::start() {
    chessBoardView->show();
}

void ChessBoardController::initBoard() { //test
    chessBoard->init();

    for(int i = 0; i < 8; ++i)
        for(int k = 0; k < 8; ++k)
            chessBoardView->draw(chessBoard->field(i, k));

}

void ChessBoardController::refresh()
{
    chessBoardView->repaint();
}

void ChessBoardController::mouseMoved(int x, int y) {
    int index[2];
    Position::coords2index(y, x, index);

    chessBoardView->setPossibleMoves(chessBoard->getPossibleMovesFrom(index[0], index[1]));
}


void ChessBoardController::resetBoard()
{
    chessBoardView->clearDrawable();
    delete chessBoard;
    chessBoard = new ChessBoard();

    connect(chessBoard, SIGNAL(repaint()), this, SLOT(refresh()));
    initBoard();
    refresh();

}


ChessBoard * ChessBoardController::getChessBoard() const {
    return chessBoard;
}

ChessBoardController::~ChessBoardController() {
    delete chessBoard;
}


